<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var map; 
    OpenLayers.Lang.setCode('en');

    function test_Control_LayerSwitcher_constructor (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.LayerSwitcher();
        t.ok( control instanceof OpenLayers.Control.LayerSwitcher, "new OpenLayers.Control.LayerSwitcher returns object" );
        t.eq( control.displayClass,  "olControlLayerSwitcher", "displayClass is correct" );
    }

    function test_Control_LayerSwitcher_draw (t) {
        t.plan( 2 );

        map = new OpenLayers.Map('map');
        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);

        var div = control.draw();
        t.ok( control.div != null, "draw makes a div" );
        t.ok( div != null, "draw returns its div" );
    }
    function test_Control_LayerSwitcher_outsideViewport (t) {
        t.plan( 4 );

        map = new OpenLayers.Map('map');
        control = new OpenLayers.Control.LayerSwitcher({'div':OpenLayers.Util.getElement('layerswitcher')});
        map.addControl(control);
        t.eq(control.div.style.width, "250px", "Div is not minimized when added.");
        t.ok(control.events.element && control.events.listeners.buttonclick, "[outside] Events instance attached to div and has buttonclick event");
        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        t.eq(control.div.style.width, "0px", "Div is minimized when added.");
        t.ok(!control.events.element && map.events.listeners.buttonclick, "[inside] Events instance not attached to div and buttonclick event registered on map");
    }
 
    function test_Control_LayerSwitcher_loadContents(t) {

        t.plan( 10 );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        markers = new OpenLayers.Layer.Markers("markers");
        map.addLayer(markers);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);

        t.ok(control.layersDiv != null, "correctly makes layers div");
        t.ok(OpenLayers.Element.hasClass(control.layersDiv, "layersDiv"),
             "layers div has class layersDiv");
        t.ok(control.baseLayersDiv != null, "correctly makes layers div");
        t.ok(OpenLayers.Element.hasClass(control.baseLayersDiv, "baseLayersDiv"),
             "base layers div has class baseLayersDiv");
        t.ok(control.dataLayersDiv != null, "correctly makes layers div");
        t.ok(OpenLayers.Element.hasClass(control.dataLayersDiv, "dataLayersDiv"),
             "data layers div has class dataLayersDiv");
        t.ok(control.maximizeDiv != null, "correctly makes resize div");
        t.ok(OpenLayers.Element.hasClass(control.maximizeDiv, "maximizeDiv"),
             "maximize div has class maximizeDiv");
        t.ok(control.minimizeDiv != null, "correctly makes resize div");
        t.ok(OpenLayers.Element.hasClass(control.minimizeDiv, "minimizeDiv"),
             "minimize div has class minmizeDiv");
    }


    function test_Control_LayerSwitcher_redraw (t) {

        t.plan( (OpenLayers.BROWSER_NAME == "opera" ? 9 : 19 ) );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        markers = new OpenLayers.Layer.Markers("markers");
        map.addLayer(markers);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);

        var wmsInput = OpenLayers.Util.getElement(control.id + "_input_" + layer.name);
        t.ok(wmsInput != null, "correctly makes an input for wms layer");
        t.eq(wmsInput.type, "radio", "wms correctly made a radio button");
        t.eq(wmsInput.name, control.id + "_baseLayers", "wms correctly named");
        t.eq(wmsInput.value, layer.name, "wms correctly valued");
        
        var markersInput = OpenLayers.Util.getElement(control.id + "_input_" + markers.name);
        t.ok(markersInput != null, "correctly makes an input for markers layer");
        t.eq(markersInput.type, "checkbox", "wms correctly made a radio button");
        t.eq(markersInput.name, markers.name, "wms correctly named");
        t.eq(markersInput.value, markers.name, "wms correctly valued");

        t.eq(false, control.checkRedraw(), "check redraw is false");
        if (OpenLayers.BROWSER_NAME != "opera") { 
            control = new OpenLayers.Control.LayerSwitcher();
            var myredraw = control.redraw;
            control.redraw = function() { 
                t.ok(true, "redraw called when setting vis");
            }
            map.addControl(control); 
            var func = OpenLayers.Function.bind(myredraw, control);
            func();
            markers.setVisibility(false); 
            t.eq(control.checkRedraw(), true, "check redraw is true after changing layer and not letting redraw happen.");
            map.removeControl(control);
            
            control = new OpenLayers.Control.LayerSwitcher();
            var myredraw = control.redraw;
            control.redraw = function() { 
                t.ok(true, "redraw called when setting inRange");
            }
            map.addControl(control); 
            var func = OpenLayers.Function.bind(myredraw, control);
            func();
            markers.inRange = false;
            t.eq(control.checkRedraw(), true, "check redraw is true after changing layer.inRange and not letting redraw happen.");
            map.removeControl(control);
            
            control = new OpenLayers.Control.LayerSwitcher();
            var myredraw = control.redraw;
            control.redraw = function() { 
                t.ok(true, "redraw called when raising base layer ");
            }
            
            map.addControl(control); 
            var func = OpenLayers.Function.bind(myredraw, control);
            func();
            map.raiseLayer(layer, 1);
            t.eq(control.checkRedraw(), true, "check redraw is true after changing layer.inRange and not letting redraw happen.");
            map.removeControl(control);
        } else {
            t.debug_print("FIXME: Some LayerSwitcher tests fail in Opera.");
        }    

    }
    function test_Control_LayerSwitcher_ascending (t) {

        t.plan( 4 );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        markers = new OpenLayers.Layer.Markers("markers");
        map.addLayer(markers);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        control2 = new OpenLayers.Control.LayerSwitcher({'ascending':false});
        map.addControl(control2);
        t.ok(control.div.childNodes[0].childNodes[0].innerHTML.match("Base Layer"), "Base Layers first in LayerSwitcher with ascending true");
        t.ok(control.div.childNodes[0].childNodes[2].innerHTML.match("Overlays"), "Overlays in LayerSwitcher with ascending true");
        t.ok(control2.div.childNodes[0].childNodes[2].innerHTML.match("Base Layer"), "Base Layers last in LayerSwitcher with ascending false");
        t.ok(control2.div.childNodes[0].childNodes[0].innerHTML.match("Overlays"), "Base Layers last in LayerSwitcher with ascending false");
    }
    
    function test_Control_LayerSwitcher_displayInLayerSwitcher (t) {

        t.plan( 2 );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"}, {'displayInLayerSwitcher': false});
        map.addLayer(layer);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        t.eq(control.div.childNodes[0].childNodes[0].style.display, "none" , "Base layer display off when no visble base layer");
        
        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        t.eq(control.div.childNodes[0].childNodes[0].style.display, "" , "Base layer display on when visble base layer");
    }



  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
    <div id="layerswitcher" style="width:250px; height:256px;" />
</body>
</html>
